package com.um.jdy.app.charge.manager;

import cn.hutool.core.bean.BeanUtil;
import com.um.jdy.app.charge.entity.dto.ComplaintDTO;
import com.um.jdy.app.charge.entity.vo.ComplaintOrderVO;
import com.um.jdy.common.base.entity.po.Problem;
import com.um.jdy.common.base.entity.vo.ProblemVO;
import com.um.jdy.common.base.service.ProblemService;
import com.um.jdy.common.charge.entity.dto.ComplaintOrderDTO;
import com.um.jdy.common.charge.entity.po.ComplaintOrder;
import com.um.jdy.common.charge.entity.po.Order;
import com.um.jdy.common.charge.service.ComplaintOrderService;
import com.um.jdy.common.charge.service.OrderService;
import com.um.jdy.common.member.service.MemberUserService;
import com.um.jdy.common.push.service.PushService;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.utils.UMBeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ComplaintOrderManager {

    @Autowired
    MemberUserService userService;

    @Autowired
    OrderService orderService;

    @Autowired
    ProblemService problemService;

    @Autowired
    ComplaintOrderService complaintOrderService;

    @Autowired
    PushService pushService;

    /**
     * 问题投诉
     * @param dto
     * @return
     */
    public Response addComplaintOrder(ComplaintDTO dto) {
        String userId = userService.get().getMemberUserId();
        Order order = orderService.getEntity(new String[]{"charge_order_id","member_user_id"},new Object[]{dto.getChargeOrderId(),userId});
        if (null == order) {
            throw new ServiceException("操作失败，订单不存在！");
        }

        ComplaintOrder complaintOrder = BeanUtil.toBean(dto, ComplaintOrder.class);
        complaintOrder.setBaseProblemId(dto.getProblemId());
        complaintOrder.setComplaintTime(new Date());
        complaintOrderService.addEntity(complaintOrder);

        pushService.pushComplaintOrder(order);
        return Response.success();
    }

    /**
     * 问题详情
     * @param id
     * @return
     */
    public ProblemVO getProblemInfo(String id) {
        Problem problem =  problemService.getEntity(id);
        return BeanUtil.toBean(problem, ProblemVO.class);
    }

    /**
     * 查询订单及问题列表
     * @param id
     * @return
     */
    public ComplaintOrderVO getComplaintOrder(String id) {
        String userId = userService.get().getMemberUserId();
        Order order = orderService.getEntity(new String[]{"charge_order_id","member_user_id"},new Object[]{id,userId});
        if (null == order) {
            throw new ServiceException("操作失败，订单不存在！");
        }

        ComplaintOrderVO orderVO = UMBeanUtils.copyBean(order,ComplaintOrderVO.class);
        List<Problem> list = problemService.getList("device_type",order.getDeviceType(),"sort_no","asc");
        orderVO.setProblems(UMBeanUtils.copyBeanList(list,ProblemVO.class));
        return orderVO;
    }
}
